package stuman.activity;

import stuman.entity.Student;
import stuman.entity.Subject;
import stuman.util.Container;
import com.StuMan.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class SubjectView extends Activity{
	
	private Student student;
	private Subject sub;
	
    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.subject_view);
        
        Bundle bun = getIntent().getExtras();	
        
        student = Container.getStudent(bun.getString("student"));
        sub = student.getSubject(bun.getString("subject"));
        
        loadSubjectInformation();	
    }
    
    @Override
	protected void onResume() {
		if (student != null){
			loadSubjectInformation();	
		}
		
		super.onResume();
	}
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.subject_menu, menu);
        return true;
    }
    
    private void loadSubjectInformation(){
    	TextView name = (TextView) findViewById(R.id.subjectName);
        name.setText("Subject: " + sub.getName().toString());
        
        TableLayout tl = (TableLayout) findViewById(R.id.sectionTable);
        tl.removeAllViews();
		for (int i=0;i<sub.getSections().size();i++){
			TableRow tr = new TableRow(this);
			final TextView tv = new TextView(this);
			tv.setTextSize(20);
			tv.setText(sub.getSections().get(i).getName());
			
			
			OnClickListener ocl = new View.OnClickListener(){

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(SubjectView.this, SectionView.class);
					intent.putExtra("section", tv.getText());
					intent.putExtra("subject", sub.getName());
					intent.putExtra("student", student.getFirstName());
				startActivity(intent);
				}
				
			};
			
			tv.setOnClickListener(ocl);
			tr.addView(tv);
			tl.addView(tr);
 
		}
    }
    
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
    	Intent intent;
        switch (item.getItemId()) {
        
        case R.id.addSection:
        	intent = new Intent(SubjectView.this, CreateSection.class);
        	intent.putExtra("student", student.getFirstName());
        	intent.putExtra("subject", sub.getName());
        	startActivity(intent);
            return true;
	
        case R.id.deleteClass:
        	student.deleteSubject(sub);
        	intent = new Intent(SubjectView.this, OpenPage.class);
        	startActivity(intent);
        	return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    
    }

}
